package com.windersgroup.querynumber.intercepter;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.windersgroup.querynumber.entity.UserIpMapping;
import com.windersgroup.querynumber.service.UserIpMappingService;

@Component
public class MyIntercepter implements HandlerInterceptor {

	@Autowired
	@Qualifier("userIpMappingService")
	private UserIpMappingService userIpMappingService;
	
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		// TODO Auto-generated method stub
		List<UserIpMapping> userList = userIpMappingService.findUserByIp(request.getRemoteAddr());
		if (!CollectionUtils.isEmpty(userList)) {
			return true;
		}
		response.sendRedirect("/machine/index/nologin");
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO Auto-generated method stub
//		HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO Auto-generated method stub
//		HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
	}
		
}